package io.qar.utils;

import cn.hutool.core.util.StrUtil;
import io.qar.config.FtpProperties;
import io.qar.entity.CfgSrcFtpFileEntity;
import io.qar.service.func.ConverterValue;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @program: qar-cloud
 * @description:
 * @author: yyyyjinying
 * @create: 2023-11-03 16:45
 **/
@Component
public class FtpUtils {
    @Autowired
    private FtpProperties ftpProperties;

    public boolean hasConditionSize(long value){
        // if (value > 100L) {
        //     return true;
        // }
        // return false;
        return true;
    }

    public boolean hasConditionValue(String value){
        for (String s : ftpProperties.getStrList()) {
            if(value.contains(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取path字符串
     * @param path \ccc\bbbb\aaa\
     * @return \ccc\bbbb\aaa
     */
    public String getSeparatorPath(String path){
        String substring = path.substring(0, path.length() - 1);
        return substring;
    }

    // String a = separator + "ccc" + separator + "bbbb" + separator + "aaa" + separator;
    // \ccc\bbbb\
    public String getBeginString(String url){
        String separator = File.separator;
        String[] urlList = url.split(File.separator + File.separator);
        StringBuilder sbuider = new StringBuilder(separator);
        for (int i = 0; i < urlList.length - 1; i++) {
            if (StrUtil.isNotEmpty(urlList[i])) {
                sbuider.append(urlList[i]);
                sbuider.append(separator);
            }
        }
        return sbuider.toString();
    }

    private void setAllSrcFileNodes(FTPClient ftpClient, String strId, String path, List<CfgSrcFtpFileEntity> nodeList, ConverterValue<CfgSrcFtpFileEntity> c) {
        path = path + File.separator;
        try {
            boolean b = ftpClient.changeWorkingDirectory(path);
            if (b) {
                FTPFile[] ftpFiles = ftpClient.listFiles();
                int num = 0;
                for (FTPFile ftpFile : ftpFiles) {
                    num++;
                    String pid = strId;
                    String id = strId + "-" + num;
                    if (ftpFile.isFile()) {
                        if (hasConditionSize(ftpFile.getSize()) && hasConditionValue(ftpFile.getName())) {
                            nodeList.add(c.getValue(ftpFile, id, pid, path, num));
                        }
                    }
                    if (ftpFile.isDirectory()) {
                        String sb = path + ftpFile.getName();
                        CfgSrcFtpFileEntity cfgSrcFtpFileEntity = c.getValue(ftpFile, id, pid, path, num);
                        nodeList.add(cfgSrcFtpFileEntity);
                        // 递归
                        setAllSrcFileNodes(ftpClient, id, sb, nodeList, c);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
